package com.ncp.servlet.registerlogin;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.ncp.model.biz.news.NewsManager;
import com.ncp.model.biz.registerlogin.LoginBiz;
import com.ncp.model.entity.registerlogin.UserBasicBean;

public class uploadMyImgServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        this.doPost(request, response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String path1 = request.getServerName();
		String path2 = request.getRealPath(path1);
		int n=path2.lastIndexOf("\\");
		String S_realPath=path2.substring(0,n);
		String realPath =S_realPath+"\\"; 
		
		boolean b1=false,b2=false;
		String s[]=new String[3];
		//设置request编码，主要是为了处理普通输入框中的中文问题
		request.setCharacterEncoding("utf-8");
		//这里对request进行封装，RequestContext提供了对request多个访问方法
		
		
		int UserID = Integer.parseInt(request.getParameter("UserID"));
        
       
		   DiskFileItemFactory factory = new DiskFileItemFactory();
		   //设置文件的缓存路径
		   factory.setRepository(new File("E:/"));
		   ServletFileUpload upload = new ServletFileUpload(factory);
		   //设置上传文件大小的上限，-1表示无上限 
		   upload.setSizeMax(-1);
		   List items = new ArrayList();
		   try {
		       //上传文件，并解析出所有的表单字段，包括普通字段和文件字段
			   items = upload.parseRequest(request);
		   } catch (FileUploadException e1) {
			//   System.out.println("文件上传发生错误" + e1.getMessage());
		    }
		    //下面对每个字段进行处理，分普通字段和文件字段
		   Iterator it = items.iterator();
		  
		 
			  
			   FileItem fileItem = (FileItem) it.next();
		    	
		    	
		    	 String tempStr1[]=NewsManager.split(fileItem.getName(),"\\");
		    	 s[0]=tempStr1[tempStr1.length-1];
		    	 String tempStr2[]=NewsManager.split(s[0],".");
		    	 s[0]=UUID.randomUUID().toString()+"."+tempStr2[tempStr2.length-1];
		    	 //s[i]=tempStr2[tempStr.length-1];
			     //保存文件，其实就是把缓存里的数据写到目标路径下
			     if(fileItem.getName()!=null && fileItem.getSize()!=0){
			     File fullFile = new File(fileItem.getName());
			    
			     File newFile = new File(realPath + "images/UserImage/"+s[0]);
			     try {
			    	 fileItem.write(newFile);
			    	 b1=true;
			     } catch (Exception e) {
			    	 
			    	
			    	 e.printStackTrace();
			    	 
			      }
			     }else{
			    	 System.out.println("文件没有选择 或 文件内容为空");
			     }
			 
		
		   
		
		
			     LoginBiz mbpb= new LoginBiz();
		
	
		   b1=mbpb.setMyImgBef(UserID,"images/UserImage/"+s[0]);
	
		   String url = "images/UserImage/"+s[0];
		   request.setAttribute("url", url);
		   LoginBiz lb = new LoginBiz();
		   UserBasicBean ubb = lb.getMyImg(UserID);
		   request.setAttribute("imgname", s[0]);
		   request.setAttribute("ubb", ubb);
		   request.getRequestDispatcher("pages/registerlogin/AddMyimg.jsp").forward(request,response);
		
			
			
		}
		
	}


